package org.jeecg.modules.online.cgform.converter.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtil;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.vo.DictModel;
import org.jeecg.common.util.SpringContextUtils;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.online.cgform.constant.ExtendJsonKey;
import org.jeecg.modules.online.cgform.converter.channel.ForeseeConvert;
import org.jeecg.modules.online.cgform.entity.OnlCgformField;
import org.jeecg.modules.online.cgform.util.CgformUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户转换
 */
public class UserSelectConverter extends ForeseeConvert {

    public UserSelectConverter(OnlCgformField onlCgformField){
        ISysBaseAPI sysBaseAPI = SpringContextUtils.getBean(ISysBaseAPI.class);
        String table = CgformUtil.USER_TABLE_NAME;
        String txt = CgformUtil.USER_REAL_NAME;
        String code = CgformUtil.USER_LOGIN_NAME;
        //update-begin-author:taoyan date:20211227 for: JTC-260(oracle下修改)【online导入导出】 部门组件配置 扩展参数{store} 导出显示空白 导入未替换成store的字段值
        String jsonStr = onlCgformField.getFieldExtendJson();
        if(StringUtil.isNotEmpty(jsonStr)){
            JSONObject json = JSON.parseObject(jsonStr);
            if(json.containsKey(ExtendJsonKey.STORE)){
                // 获取的是 驼峰规则的
                String camelCode = json.getString(ExtendJsonKey.STORE);
                code = oConvertUtils.camelToUnderline(camelCode);
            }

            if(json.containsKey(ExtendJsonKey.TEXT)){
                // 获取的是 驼峰规则的
                String camelTxt = json.getString(ExtendJsonKey.TEXT);
                txt = oConvertUtils.camelToUnderline(camelTxt);
            }
        }
        //update-end-author:taoyan date:20211227 for: JTC-260(oracle下修改)【online导入导出】 部门组件配置 扩展参数{store} 导出显示空白 导入未替换成store的字段值
        List<DictModel> dictList = sysBaseAPI.queryTableDictItemsByCode(table,txt,code);
        this.dictList = dictList;
        this.filed = onlCgformField.getDbFieldName();
    }

    @Override
    public String converterToVal(String txt) {
        if(oConvertUtils.isEmpty(txt)){
            return null;
        }
        List<String> res = new ArrayList<String>();
        for(String temp:txt.split(",")){
            String switchvalue = super.converterToVal(temp);
            if(switchvalue==null){
                continue;
            }
            res.add(switchvalue);
        }
        return String.join(",", res);
    }

    @Override
    public String converterToTxt(String val) {
        if(oConvertUtils.isEmpty(val)){
            return null;
        }
        List<String> res = new ArrayList<String>();
        for(String temp:val.split(",")){
            String switchTxt = super.converterToTxt(temp);
            if(switchTxt==null){
                continue;
            }
            res.add(switchTxt);
        }
        return String.join(",", res);
    }

}
